<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.9
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############

/**
 * Makes a drop menu
 * @author Anthony Gallon
 * @package Antz
 */

/**
 * Makes a drop menu
 * @author Anthony Gallon
 * @package Antz
 */

class JFX_Form_Components_Select extends JFX_Form_Component
{
    /**
     * The options or opt-group objects
     * @var array $options
     */
    protected $options = array();

    /**
     * The type of this object
     * @var string $type
     */
    protected $type = 'select';

    /**
     * The value that is pre selected
     * @var array $value
     */
    protected $value = array();
    
    /**
     * Allow multiple values to be selected?
     * @var bool $multiple
     */
    protected $multiple = false;
    
    /**
     * The size of the drop menu if using multiple
     * @var int $size
     */
    protected $size = 1;

    /**
     * Set our initial values
     * @param string $name
     * @param string $label
     * @param mixed $value
     * @param string $afterText
     */
    public function __construct($name, $label, $value='', $afterText=''){
        $this->id = 'sel'.rand();
        if(!is_array($value)) $value = array($value);
        $this->aftertext = $afterText;
        $this->name = $name;
        $this->label = $label;
        $this->value = $value;
        $this->isXhtml(false);
    }

    /**
     * Add an option to the drop menu
     * @param string $title
     * @param string $value
     * @return object JFX_Form_Components_Option
     */
    public function add($title, $value=''){
		if($title instanceof JFX_Form_Components_Option){
			$this->options[] = $title;
			return $this->options[(count($this->options)-1)];
		}else{
			if($value==='')$value = $title;
			$selected = (in_array($value, $this->value)) ? true : false;
			$this->options[] = new JFX_Form_Components_Option($title, $value, $selected);
			return $this->options[(count($this->options)-1)];
		}
        
    }

    /**
     * Set if it is a multiple select
     * @param bool $bool
     */
    public function setMultiple($bool){
      $this->multiple = (bool) $bool;
    }

    /**
     * Set the size of the drop menu if using multiple select
     * @param int $size
     */
    public function setSize($size){
      $this->size = (int) $size;
    }

    /**
     * Add an opt-group to the form
     * @param string $title
     * @param mixed $value
     * @return object JFX_Form_Components_OptGroup
     */
    public function addOptGroup($title, $value=''){
    	if($value=='') $value = $this->value;
    	else if(!is_array($value)) $value = array($value);
        $this->options[] = new JFX_Form_Components_OptGroup($title, $value);
        return $this->options[count($this->options)-1];
    }

    /**
     * Returnt the drop menu as HTML text
     * @global object $gCms
     * @return string $html
     */
    public function render(){
    	$CONFIG = JFX::registry('config');
    	  $size = 'size="'.$this->size.'"';
        $disabled = ($this->disabled) ? 'disabled="disabled"' : '';
        $multiple = ($this->multiple) ? 'multiple="multiple"' : '';
        if(JFX::isFormError($this->name)) $this->addClass('error');
        if($this->required) $this->aftertext = '<span class="required"><img src="'.$CONFIG->baseUrl.'/jfx/images/required.gif" alt="Required" /></span>'.$this->aftertext;
        $returnString = "\n".'<select name="'.$this->name.'" id="'.$this->getId().'" '.$this->renderParameters().$this->renderClasses().$disabled.' '.$multiple.' '.$size.'>';
        foreach($this->options as $option){
            $returnString .= "\n  ".$option->render();
        };
        if($this->showLabel){
            $returnString = '<label for="'.$this->getId().'">'.$this->label.'</label>'.$returnString;
        }
        $returnString .= "\n".'</select><div class="aftertext">'.$this->aftertext.'</div>';
        if($this->wrap) $returnString = $this->wrap($returnString);
        return $returnString;
    }

    /**
     * Cancel the opt-group status and create an option next time
     */
    public function breakOptGroup(){
        $this->optGroupOpen = false;
    }

}
